/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.beans.PropertyVetoException;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.apache.log4j.Logger;


public class JFrameWizard extends JInternalFrame {

    private static Logger logger = Logger.getLogger(JFrameWizard.class);  	
	
	private static final int NB_SCREENS =4;	
	
	public static final int MAP_SCREEN =0;
	public static final int ATTRIBUTES_SCREEN =1;
	public static final int CD_SCREEN =2;
	public static final int WIZARD_FINISH_SCREEN =3;
	
	private int screenCt;
	
	private JFrameThematicMap jFrameThematicMap =null;
	
	private JPanel jContentPane = null;
	private JPanel jPanelComment = null;
	private JPanel jPanelButtons = null;
	private JButton jButtonBack = null;
	private JButton jButtonNext = null;
	private JButton jButtonCancel = null;
	private JButton jButtonFinish = null;
	private JLabel jLabelComment1 = null;
	private JPanel jPanelSpecificButtons = null;
	private JPanel jPanelNavButtons = null;
	private JButton jButtonOpenMap = null;
	private JButton jButtonSaveThematicMap = null;
	private JButton jButtonPrintThematicMap = null;
	private JButton jButtonOpenData = null;
	private JButton jButtonImportData = null;
	private JButton jButtonNewMap = null;
	private JButton jButtonNewData = null;

	/**
	 * This is the default constructor
	 */
	public JFrameWizard() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		//this.setSize(413, 200);
		this.setTitle("TMap Wizard");
		this.setContentPane(getJContentPane());

	}
	
	public void startWizard() {
		screenCt =MAP_SCREEN;
		displayPanelInfo();
	}
	
	public void setJFrameThematicMap(JFrameThematicMap frameThematicMap) {
		jFrameThematicMap = frameThematicMap;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelComment(), BorderLayout.NORTH);
			jContentPane.add(getJPanelButtons(), BorderLayout.CENTER);

		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanelComment	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelComment() {
		if (jPanelComment == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1);
			jLabelComment1 = new JLabel();
			jLabelComment1.setHorizontalAlignment(SwingConstants.CENTER);
			jPanelComment = new JPanel();
			jPanelComment.setLayout(gridLayout);
			jPanelComment.add(jLabelComment1, null);
		}
		return jPanelComment;
	}

	/**
	 * This method initializes jPanelButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(2);
			jPanelButtons = new JPanel();
			jPanelButtons.setLayout(gridLayout1);
			jPanelButtons.add(getJPanelSpecificButtons(), null);
			jPanelButtons.add(getJPanelNavButtons(), null);
		}
		return jPanelButtons;
	}
	
	/**
	 * This method initializes jPanelSpecificButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelSpecificButtons() {
		if (jPanelSpecificButtons == null) {
			jPanelSpecificButtons = new JPanel();
			jPanelSpecificButtons.add(getJButtonNewMap(), null);
			jPanelSpecificButtons.add(getJButtonOpenMap(), null);
			jPanelSpecificButtons.add(getJButtonNewData(), null);
			jPanelSpecificButtons.add(getJButtonOpenData(), null);
			jPanelSpecificButtons.add(getJButtonImportData(), null);
			jPanelSpecificButtons.add(getJButtonSaveThematicMap(), null);
			jPanelSpecificButtons.add(getJButtonPrintThematicMap(), null);
		}
		return jPanelSpecificButtons;
	}

	/**
	 * This method initializes jPanelNavButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelNavButtons() {
		if (jPanelNavButtons == null) {
			jPanelNavButtons = new JPanel();
			jPanelNavButtons.add(getJButtonBack(), null);
			jPanelNavButtons.add(getJButtonNext(), null);
			jPanelNavButtons.add(getJButtonCancel(), null);
			jPanelNavButtons.add(getJButtonFinish(), null);		
		}
		return jPanelNavButtons;
	}		

	/**
	 * This method initializes jButtonPrevious	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonBack() {
		if (jButtonBack == null) {
			jButtonBack = new JButton();
			jButtonBack.setText( "<< Back" );
			jButtonBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if ( screenCt > 0 ) {
						--screenCt;
						displayPanelInfo();
					}    
				}
			});
		}
		return jButtonBack;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNext() {
		if (jButtonNext == null) {
			jButtonNext = new JButton();
			jButtonNext.setText("Next >>");
			jButtonNext.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if ( screenCt < NB_SCREENS ) {    
						screenCt++;
						displayPanelInfo();
					}				
				}
			});
		}
		return jButtonNext;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText( "Cancel" );
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.getThematicMapUIHelper().newThematicMap( getContentPane(), false );
					jFrameThematicMap.stopWizardMode();	
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jButtonFinish	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonFinish() {
		if (jButtonFinish == null) {
			jButtonFinish = new JButton();
			jButtonFinish.setText( "Finish" );
			jButtonFinish.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.stopWizardMode();				
				}
			});
		}
		return jButtonFinish;
	}
	
	private void displayPanelInfo() {		
		getJButtonBack().setVisible( screenCt > 0 );
		getJButtonNext().setVisible( screenCt != NB_SCREENS - 1 );
		getJButtonFinish().setVisible( screenCt == NB_SCREENS - 1 );
		
		switch( screenCt ) {	
			
			case MAP_SCREEN:
				jLabelComment1.setText( screenCt + 1 + "- Welcome to the Thematic Map Wizard - Please select one or more base maps");

				getJButtonNewMap().setVisible( true );
				getJButtonOpenMap().setVisible( true );
				getJButtonNewData().setVisible( false );
				getJButtonOpenData().setVisible( false );
				getJButtonImportData().setVisible( false );
				getJButtonSaveThematicMap().setVisible( false );
				getJButtonPrintThematicMap().setVisible( false );				
				getJPanelSpecificButtons().setVisible( true );
				
				break;
				
			case ATTRIBUTES_SCREEN:
				jLabelComment1.setText( screenCt + 1 + "- Please load or create attributes");

				getJButtonNewMap().setVisible( false );				
				getJButtonOpenMap().setVisible( false );
				getJButtonNewData().setVisible( true );
				getJButtonOpenData().setVisible( true );
				getJButtonImportData().setVisible( true );
				getJButtonSaveThematicMap().setVisible( false );
				getJButtonPrintThematicMap().setVisible( false );				
				getJPanelSpecificButtons().setVisible( true );				
				break;
				
			case CD_SCREEN:
				jLabelComment1.setText( screenCt + 1 + "- Please specify cartographic display");
				
				getJPanelSpecificButtons().setVisible( false );								
				break;				
				
			case WIZARD_FINISH_SCREEN:
				jLabelComment1.setText( screenCt + 1 + "- Final step: Save or print the thematic map");			
				
				getJButtonNewMap().setVisible( false );				
				getJButtonOpenMap().setVisible( false );
				getJButtonNewData().setVisible( false );			
				getJButtonOpenData().setVisible( false );
				getJButtonImportData().setVisible( false );				
				getJButtonSaveThematicMap().setVisible( true );
				getJButtonPrintThematicMap().setVisible( true );				
				getJPanelSpecificButtons().setVisible( true );				
				break;				
		}
		
		try {
			jFrameThematicMap.displayWizardFrame( screenCt );
		} catch (PropertyVetoException e) {
			logger.error( e );
		}
	}

	/**
	 * This method initializes jButtonOpenMap	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOpenMap() {
		if (jButtonOpenMap == null) {
			jButtonOpenMap = new JButton();
			jButtonOpenMap.setText("Open map...");
			jButtonOpenMap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
				    jFrameThematicMap.openMap();
				}
			});
		}
		return jButtonOpenMap;
	}

	/**
	 * This method initializes jButtonSaveThematicMap	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSaveThematicMap() {
		if (jButtonSaveThematicMap == null) {
			jButtonSaveThematicMap = new JButton();
			jButtonSaveThematicMap.setText("Save thematic map...");
			jButtonSaveThematicMap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.saveThematicMap();
				}
			});
		}
		return jButtonSaveThematicMap;
	}

	/**
	 * This method initializes jButtonPrintThematicMap	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonPrintThematicMap() {
		if (jButtonPrintThematicMap == null) {
			jButtonPrintThematicMap = new JButton();
			jButtonPrintThematicMap.setText("Print thematic map...");
			jButtonPrintThematicMap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.printThematicMap();
				}
			});
		}
		return jButtonPrintThematicMap;
	}

	/**
	 * This method initializes jButtonOpenAttributes	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOpenData() {
		if (jButtonOpenData == null) {
			jButtonOpenData = new JButton();
			jButtonOpenData.setText("Open data...");
			jButtonOpenData.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.openData();
				}
			});
		}
		return jButtonOpenData;
	}

	/**
	 * This method initializes jButtonImportAttributes	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonImportData() {
		if (jButtonImportData == null) {
			jButtonImportData = new JButton();
			jButtonImportData.setText("Import data...");
			jButtonImportData.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.importData();
				}
			});
		}
		return jButtonImportData;
	}

	/**
	 * This method initializes jButtonNewMap	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNewMap() {
		if (jButtonNewMap == null) {
			jButtonNewMap = new JButton();
			jButtonNewMap.setText("New map");
			jButtonNewMap.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.newMap();
				}
			});
		}
		return jButtonNewMap;
	}

	/**
	 * This method initializes jButtonNewAttributes	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNewData() {
		if (jButtonNewData == null) {
			jButtonNewData = new JButton();
			jButtonNewData.setText("New data");
			jButtonNewData.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jFrameThematicMap.newData();
				}
			});
		}
		return jButtonNewData;
	}



}  //  @jve:decl-index=0:visual-constraint="10,10"
